package toolkit;

import inputstream.StringStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Vector;


/**legge una database di stringhe in un file e le immagazzina; in seguito e' possibile leggere tutte le stirnghe immagazzinate
 * 
 * @author koldar
 *
 */
public class StoreReader extends StringStream{

	
	protected int currentnumber;
	protected Vector<String> stringstore;
	
	public StoreReader(InputStream _sourcestream) throws IOException {
		super(_sourcestream, StringStream.STRINGSEPARATORDEFAULT);
		this.currentnumber=0;
		this.stringstore=this.memorizeExternalArchive();
		this.sourcestream.close();
	}
	
	@Override
	public String readString(){
		return this.stringstore.get(this.currentnumber++);
	}
	@Override
	public <NUMBER extends Number> NUMBER readNumber(Class<NUMBER> numbertype){
		String tobeconverted=this.readString();
		
		if (numbertype.getSuperclass()!=Number.class){
			this.inputerror=StringStream.PARSERNOTFOUND;
			return null;
		}
		return this.getNumberFromClass(numbertype, tobeconverted);
	}

	private Vector<String> memorizeExternalArchive(){
		Vector<String> temp=new Vector<String> (0,1);
		do{
			try{
				temp.add(super.readString());
			}
			catch (NoSuchElementException e){
				break;
			}
		}while(true);
		return temp;
	}
	/**
	 * @return the currentnumber
	 */
	public int getCurrentnumber() {
		return currentnumber;
	}
	/**
	 * @return the stringstore
	 */
	public Vector<String> getStringstore() {
		return stringstore;
	}
	
}
